#include<iostream>
#include<stdio.h>
using namespace std;
int a[100005];
int num(int a)
{
	int n=0;
	while(a!=0)
	{
		if(a%2==1)
			n++;
		a/=2;
	}
	return n;
}
int main()
{
	int i;
	while(cin>>i)
	{
		if(i==0)
			break;
		for (int a=i+1;;a++)
		{
			if(num(a)==num(i))
			{
				printf("%d\n",a);
				break;
			}
		}
	}
	return 0;
}

